package com.ews.onlineexchange.controller;

import java.util.Date;

import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.Query;

import com.ews.onlineexchange.model.*;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import com.ews.onlineexchange.service.CategoryHome;
import com.ews.onlineexchange.service.EntityHome;
import com.ews.onlineexchange.service.UserService;
import com.ews.onlineexchange.util.AttachmentUtils;

@Named
@ConversationScoped
public class ArticleHome extends EntityHome<Article, String> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Inject
	UserService userService;
	
	@Inject
	CategoryHome categoryHome;
	
	private UploadedFile uploadFile;
	
	@Inject
	AttachmentUtils attachmentUtils;
	public void uploadImage(FileUploadEvent event) {
		if (event.getFile() == null)
			return ;
		Image attr = attachmentUtils.getImageFromUpload(event.getFile());
		getInstance().setImage(attr);
		FacesMessage message = new FacesMessage(event.getFile().getFileName() + " 上传成功。");
        FacesContext.getCurrentInstance().addMessage(null, message);
	}

	@Override
	public String saveAndFlush() {
		getInstance().setCategory(categoryHome.getInstance());
		
		if (this.getUploadFile() != null) {
			com.ews.onlineexchange.model.Image attr = attachmentUtils.getImageFromUpload(getUploadFile());
			getInstance().setImage(attr);
		}
		
		if(isManaged())
			getInstance().setEditDate(new Date());
		User loginedUser = userService.getLoginedUser();
		String publisher = null;
		if (loginedUser == null) {
			publisher = "系统管理员";
		} else {
			publisher = loginedUser.getCnname();
		}
		getInstance().setPublisher(publisher);
		
		return super.saveAndFlush();
	}
	
	@Override
	public String delete() {
		getInstance().setDelFlag(DataEntity.DEL_FLAG_DELETE);
		return super.saveAndFlush();
	}
	
	private static final String UPDATE_ARTICLE_HITS_EQL = ""
			+ "	update 	Article article"
			+ " set article.hits =  article.hits+1 "
			+ "	WHERE	article = :articleId ";
	public void updateArticleHit(String articleId) {
		Query query = getEntityManager().createQuery(UPDATE_ARTICLE_HITS_EQL);
		query.setParameter("articleId", articleId);
		query.executeUpdate();
		
	}
	public String audit() {
		getInstance().setStat("1");
		return super.saveAndFlush();
		
	}

	public UploadedFile getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(UploadedFile uploadFile) {
		this.uploadFile = uploadFile;
	}
	
	public void removeAttachment(Attachment att) {
		getInstance().getAttachment().remove(att);
	}
	public void uploadAttachments(FileUploadEvent event) {
		if (event.getFile() == null)
			return ;
		
		Attachment attr = attachmentUtils.getAttachmentFromUpload(event.getFile());
		getInstance().getAttachment().add(attr);
		FacesMessage message = new FacesMessage(event.getFile().getFileName() + " 上传成功。");
        FacesContext.getCurrentInstance().addMessage(null, message);
	}
	
}
